<template>
	<div class="system-dic-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-user-search mb15">
				<el-button size="default" type="success" @click="onOpenAdd('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增AI机器码
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column prop="remark" label="名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="key" label="AI机器码" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="150">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<AddEditDialog ref="addEditDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup name="extends_accredit">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { systemMachineApi } from '/@/api/machine/index';
// 引入组件
const AddEditDialog = defineAsyncComponent(() => import('./dialog.vue'));
// 定义变量内容
const addEditDialogRef = ref();
const state = reactive({
	tableData: {
		data: [],
		total: 0,
		loading: false,
	},
});

// 初始化表格数据
const getTableData = () => {
	state.tableData.loading = true;
	systemMachineApi().indexAi().then(res=>{
		state.tableData.data = res.data;
	}).finally(() => {
		state.tableData.loading = false;
	});
};
// 打开新增弹窗
const onOpenAdd = (type) => {
	addEditDialogRef.value.openDialog(type);
};
// 打开修改弹窗
const onOpenEdit = (type, row) => {
	addEditDialogRef.value.openDialog(type, row);
};
// 删除
const onRowDel = (row) => {
	ElMessageBox.confirm(`此操作将永久删除该AI机器码：“${row.key}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(() => {
			systemMachineApi().delAi([row.key]).then(res=>{
                getTableData();
                ElMessage.success('删除成功');
            })
	}).catch(() => {});
};

const seachClick = ()=>{
	onHandleCurrentChange(1)
}
// 分页改变
const onHandleSizeChange = (val) => {
	state.tableData.param.limit = val;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (val) => {
	console.log(12);
	state.tableData.param.page = val;
	getTableData();
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>
